package com.qf.mrmd.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.UUID;

public class Sms {
    /*
    pom.xml
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.5.16</version>
    </dependency>
    */
        public static String SmsCode(String phoneNumber) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tAJVjcRL7fHQ5oNA85k", "1MPBVefrqHPzNTYP98RSAziQtdXCFZ");
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("PhoneNumbers", phoneNumber);
            request.putQueryParameter("SignName", "阳阳商城");
            request.putQueryParameter("TemplateCode", "SMS_205409285");
            //request.putQueryParameter("TemplateParam", "231564");
            HashMap<String,String> map = new HashMap<>();
            String code = UUID.randomUUID().toString().substring(0, 4);
            map.put("code",code);
            request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

            try {
                CommonResponse response = client.getCommonResponse(request);
                String data = response.getData();
                JSONObject jsonObject = JSONObject.parseObject(data);
                String status = jsonObject.getString("Code");
                if (status.equalsIgnoreCase("ok")){
                    return code;
                }else{
                    return null;
                }
            } catch (ClientException e) {
                e.printStackTrace();
                return null;
            }
        }


}
